package com.developer.platform.domain.listener;

import com.developer.platform.domain.event.MenuEvent;
import com.developer.platform.domain.event.MenuEventPublisher;
import com.developer.platform.domain.service.IAuthorizationSource;
import com.developer.platform.domain.service.IAuthorizingRealm;
import com.google.common.eventbus.Subscribe;
import org.springframework.stereotype.Component;

/**
 * 菜单异步事件监听
 *
 * @author ：qiaoliang
 * @date ：2020-11-04
 */
@Component
public class MenuEventListener {

    private final IAuthorizationSource authorizationSource;
    private final IAuthorizingRealm authorizingRealm;

    public MenuEventListener(MenuEventPublisher menuEventPublisher,
                             IAuthorizationSource authorizationSource,
                             IAuthorizingRealm authorizingRealm) {
        this.authorizationSource = authorizationSource;
        this.authorizingRealm = authorizingRealm;
        menuEventPublisher.register(this);
    }

    @Subscribe
    public void asyncPublishMenuChange(MenuEvent menuEvent){
        // 初始化全局安全拦截
        authorizationSource.initializationResourceDefine();
        // 初始化用户可访问资源
        authorizingRealm.initializationResourceDefine();
    }

}
